package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

public class FindStudent {

	public static void main(String[] args) {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			return;
		}
		String url = "jdbc:mysql://localhost:3306/java46?useUnicode=true&characterEncoding=utf8";
		Connection con = null;
		Statement st = null;
		ResultSet rs = null;
		try {
			con = DriverManager.getConnection(url, "suncy", "suncy");
			st = con.createStatement();
			rs = st.executeQuery("select stuno,sname as studentname,telephone,birthday,fromcity as city from student order by birthday desc");
			while (rs.next()) {
				int no = rs.getInt(1);
				String name = rs.getString("studentname");
				String tele = rs.getString("telephone");
				Date d = rs.getDate("birthday");
				String city = rs.getString("city");
				System.out.println(no + "," + name + "," + tele + "," + d + ","
						+ city);
			}
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} finally {
			if (rs != null)
				try {
					rs.close();
				} catch (SQLException e2) {
					// TODO 自动生成的 catch 块
					e2.printStackTrace();
				}
			try {
				if (st != null)
					st.close();
			} catch (SQLException e1) {
				// TODO 自动生成的 catch 块
				e1.printStackTrace();
			}

			try {
				if (con != null)
					con.close();
			} catch (SQLException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}

		}

	}

}
